﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using DalNHibernat.Implementacao;

namespace DalNHibernat
{
    public abstract class ApplicationBase
    {
        //public abstract class ApplicationBase
        private static ISessionFactory _sessionFactory;

        private readonly ISession _session;

        public ISession Session
        {
            get { return _session; }
        }

        protected ApplicationBase()
        {
            _session = GetCurrentSessionFactory().OpenSession();
        }

        private static ISessionFactory GetCurrentSessionFactory()
        {
            if (_sessionFactory == null)
                _sessionFactory = BuildSessionFactory();
            return _sessionFactory;
        }

        private static ISessionFactory BuildSessionFactory()
        {
            try
            {
                return Fluently
                    .Configure()
                    .Database(MsSqlConfiguration.MsSql2000
                    .ConnectionString(c => c.FromConnectionStringWithKey("shippingIDConnection")))
                    .Mappings(M => M.FluentMappings.AddFromAssembly(typeof(DalCliente).Assembly))
                    .BuildSessionFactory();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
 }

